package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.common.ConfigConstant;
import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.common.RetEnum;
import com.ltxtiyu.video.service.ms.AreaService;
import com.ltxtiyu.video.service.ms.CommonService;
import com.ltxtiyu.video.service.ms.ConfigService;
import com.ltxtiyu.video.service.ms.VideoService;
import com.ltxtiyu.video.tool.AliTool;
import com.ltxtiyu.video.tool.AliVideoTool;
import com.ltxtiyu.video.vo.ms.SelectVO;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 公共接口
 */
@RequestMapping("common")
@Controller
public class CommonController {
    @Reference
    private AreaService areaService;
    @Reference
    private ConfigService configService;
    @Reference
    private VideoService videoService;
    /**
     * 上传图片获取id和url
     * @param request
     * @return
     */
    @RequestMapping("upload")
    public ResponseEntity<Map<String,Object>> upload(HttpServletRequest request){
        try {
            String OSS_ENDPOINT = configService.selectByCkey(ConfigConstant.OSS_ENDPOINT).getCvalue();
            String ACCESS_ID = configService.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
            String ACCESS_KEY = configService.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
            String BUCKET_ID = configService.selectByCkey(ConfigConstant.BUCKET_ID).getCvalue();
            String OSS_CDN = configService.selectByCkey(ConfigConstant.OSS_CDN).getCvalue();
            // 上传图片用OSS_ENDPOINT
            AliTool aliTool = AliTool.getInstance(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
            // 查看图片用OSS_CDN
            AliTool showAliTool = AliTool.getInstance(OSS_CDN, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
            String picId = AliTool.getCirclePicObjectId(request,aliTool);
            Map<String,Object> result = new HashMap<>();
            result.put("picId",picId);
            result.put("url",showAliTool.getAccessURL(picId));
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }


    //查询省市
    @RequestMapping("selectAllCity")
    public ResponseEntity< List<SelectVO>> selectAllCity(){
        try {
            List<SelectVO> result =areaService.selectAll();
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

}
